package com.example.hotHub.service.impl;


import com.example.hotHub.enums.AstroEnum;
import com.example.hotHub.model.Astro;
import com.example.hotHub.service.AstroService;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author aaa
 */
@Service
public class AstroServiceImpl implements AstroService {

    private int getNumber(String str) {
        char[] array = str.toCharArray();
        int flag = 0;
        int result = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= '0' && array[i] <= '9') {
                flag = 1;
                result =result * 10 + array[i] - '0';
            } else {
                if (flag == 1) {
                    break;
                }
            }
        }
        return result;
    }

    private AstroEnum getAstroEnum(String name) {
        List<AstroEnum> astroEnumList = Arrays.stream(AstroEnum.values()).collect(Collectors.toList());
        for (AstroEnum astroEnum : astroEnumList) {
            if (astroEnum.getName().equals(name)) {
                return astroEnum;
            }
        }
        return null;
    }

    @SneakyThrows
    @Override
    public Astro getAstroToday(String astroName) {
        String url = "https://www.xzw.com/fortune/" + astroName + "/";

        Document document = Jsoup.connect(url).get();
        System.out.println("send request to " + url);
        Elements lis = document.getElementsByClass("c_main").get(0).select("dd ul li");

        Integer generalLevel = getNumber(lis.get(0).select("span em").get(0).attr("style")) / 16;
        Integer loveLevel = getNumber(lis.get(1).select("span em").get(0).attr("style")) / 16;
        Integer careerLevel = getNumber(lis.get(2).select("span em").get(0).attr("style")) / 16;
        Integer richLevel = getNumber(lis.get(3).select("span em").get(0).attr("style")) / 16;
        Integer healthLevel = getNumber(lis.get(4).ownText()) / 20;
        Integer businessLevel = getNumber(lis.get(5).ownText()) / 20;
        String luckyColor = lis.get(6).ownText();
        String luckyNumber = lis.get(7).ownText();
        String matchAstro = lis.get(8).ownText();
        String shortComment = lis.get(9).ownText();

        Elements contents = document.getElementsByClass("c_cont").get(0).select("p span");

        String generalContent = contents.get(0).ownText();
        String loveContent = contents.get(1).ownText();
        String careerContent = contents.get(2).ownText();
        String richContent = contents.get(3).ownText();
        String healthContent = contents.get(4).ownText();

        Astro astro = new Astro();
        astro.setGeneralLevel(generalLevel);
        astro.setLoveLevel(loveLevel);
        astro.setCareerLevel(careerLevel);
        astro.setRichLevel(richLevel);
        astro.setHealthLevel(healthLevel);
        astro.setBusinessLevel(businessLevel);
        astro.setLuckyColor(luckyColor);
        astro.setLuckyNumber(luckyNumber);
        astro.setMatchAstro(matchAstro);
        astro.setShortComment(shortComment);
        astro.setGeneralContent(generalContent);
        astro.setLoveContent(loveContent);
        astro.setCareerContent(careerContent);
        astro.setRichContent(richContent);
        astro.setHealthContent(healthContent);

        AstroEnum astroEnum = getAstroEnum(astroName);
        if (astroEnum != null) {
            astro.setLabel(astroEnum.getLabel());
            astro.setName(astroEnum.getName());
            astro.setBeginMonth(astroEnum.getBeginMonth());
            astro.setBeginDay(astroEnum.getBeginDay());
            astro.setEndMonth(astroEnum.getEndMonth());
            astro.setEndDay(astroEnum.getEndDay());
        }

        return astro;
    }

    @SneakyThrows
    @Override
    public Astro getAstroTomorrow(String astroName) {
        String url = "https://www.xzw.com/fortune/" + astroName + "/1.html";

        Document document = Jsoup.connect(url).get();
        System.out.println("send request to " + url);
        Elements lis = document.getElementsByClass("c_main").get(0).select("dd ul li");

        Integer generalLevel = getNumber(lis.get(0).select("span em").get(0).attr("style")) / 16;
        Integer loveLevel = getNumber(lis.get(1).select("span em").get(0).attr("style")) / 16;
        Integer careerLevel = getNumber(lis.get(2).select("span em").get(0).attr("style")) / 16;
        Integer richLevel = getNumber(lis.get(3).select("span em").get(0).attr("style")) / 16;
        Integer healthLevel = getNumber(lis.get(4).ownText()) / 20;
        Integer businessLevel = getNumber(lis.get(5).ownText()) / 20;
        String luckyColor = lis.get(6).ownText();
        String luckyNumber = lis.get(7).ownText();
        String matchAstro = lis.get(8).ownText();
        String shortComment = lis.get(9).ownText();

        Elements contents = document.getElementsByClass("c_cont").get(0).select("p span");

        String generalContent = contents.get(0).ownText();
        String loveContent = contents.get(1).ownText();
        String careerContent = contents.get(2).ownText();
        String richContent = contents.get(3).ownText();
        String healthContent = contents.get(4).ownText();

        Astro astro = new Astro();
        astro.setGeneralLevel(generalLevel);
        astro.setLoveLevel(loveLevel);
        astro.setCareerLevel(careerLevel);
        astro.setRichLevel(richLevel);
        astro.setHealthLevel(healthLevel);
        astro.setBusinessLevel(businessLevel);
        astro.setLuckyColor(luckyColor);
        astro.setLuckyNumber(luckyNumber);
        astro.setMatchAstro(matchAstro);
        astro.setShortComment(shortComment);
        astro.setGeneralContent(generalContent);
        astro.setLoveContent(loveContent);
        astro.setCareerContent(careerContent);
        astro.setRichContent(richContent);
        astro.setHealthContent(healthContent);

        AstroEnum astroEnum = getAstroEnum(astroName);
        if (astroEnum != null) {
            astro.setLabel(astroEnum.getLabel());
            astro.setName(astroEnum.getName());
            astro.setBeginMonth(astroEnum.getBeginMonth());
            astro.setBeginDay(astroEnum.getBeginDay());
            astro.setEndMonth(astroEnum.getEndMonth());
            astro.setEndDay(astroEnum.getEndDay());
        }

        return astro;
    }

    @SneakyThrows
    @Override
    public Astro getAstroWeek(String astroName) {
        String url = "https://www.xzw.com/fortune/" + astroName + "/2.html";
        Document document = Jsoup.connect(url).get();
        System.out.println("send request to " + url);
        Elements lis = document.getElementsByClass("c_main").get(0).select("dd ul li");

        Integer generalLevel = getNumber(lis.get(0).select("span em").get(0).attr("style")) / 16;
        Integer loveLevel = getNumber(lis.get(1).select("span em").get(0).attr("style")) / 16;
        Integer careerLevel = getNumber(lis.get(2).select("span em").get(0).attr("style")) / 16;
        Integer richLevel = getNumber(lis.get(3).select("span em").get(0).attr("style")) / 16;
        Integer healthLevel = getNumber(lis.get(4).select("span em").get(0).attr("style")) / 16;
        String luckyColor = lis.get(5).ownText();
        String luckyAstro = lis.get(6).ownText();
        String dangerAstro = lis.get(7).ownText();
        String shortComment = lis.get(8).ownText();

        Elements contents = document.getElementsByClass("c_cont").get(0).select("p span");

        String generalContent = contents.get(0).ownText();
        String loveContent = contents.get(1).ownText();
        String careerContent = contents.get(2).ownText();
        String richContent = contents.get(3).ownText();
        String healthContent = contents.get(4).ownText();

        Astro astro = new Astro();
        astro.setGeneralLevel(generalLevel);
        astro.setLoveLevel(loveLevel);
        astro.setCareerLevel(careerLevel);
        astro.setRichLevel(richLevel);
        astro.setHealthLevel(healthLevel);
        astro.setLuckyColor(luckyColor);
        astro.setLuckyAstro(luckyAstro);
        astro.setDangerAstro(dangerAstro);
        astro.setShortComment(shortComment);
        astro.setGeneralContent(generalContent);
        astro.setLoveContent(loveContent);
        astro.setCareerContent(careerContent);
        astro.setRichContent(richContent);
        astro.setHealthContent(healthContent);

        AstroEnum astroEnum = getAstroEnum(astroName);
        if (astroEnum != null) {
            astro.setLabel(astroEnum.getLabel());
            astro.setName(astroEnum.getName());
            astro.setBeginMonth(astroEnum.getBeginMonth());
            astro.setBeginDay(astroEnum.getBeginDay());
            astro.setEndMonth(astroEnum.getEndMonth());
            astro.setEndDay(astroEnum.getEndDay());
        }

        return astro;
    }

    @SneakyThrows
    @Override
    public Astro getAstroMonth(String astroName) {
        String url = "https://www.xzw.com/fortune/" + astroName + "/3.html";
        Document document = Jsoup.connect(url).get();
        System.out.println("send request to " + url);
        Elements lis = document.getElementsByClass("c_main").get(0).select("dd ul li");

        Integer generalLevel = getNumber(lis.get(0).select("span em").get(0).attr("style")) / 16;
        Integer loveLevel = getNumber(lis.get(1).select("span em").get(0).attr("style")) / 16;
        Integer careerLevel = getNumber(lis.get(2).select("span em").get(0).attr("style")) / 16;
        Integer richLevel = getNumber(lis.get(3).select("span em").get(0).attr("style")) / 16;
        Integer healthLevel = getNumber(lis.get(4).select("span em").get(0).attr("style")) / 16;
        String shortComment = lis.get(5).ownText();

        Elements contents = document.getElementsByClass("c_cont").get(0).select("p span");

        String generalContent = contents.get(0).ownText();
        String loveContent = contents.get(1).ownText();
        String careerContent = contents.get(2).ownText();
        String richContent = contents.get(3).ownText();
        String healthContent = contents.get(4).ownText();
        String relaxTip = contents.get(5).ownText();
        String luckyTip = contents.get(6).ownText();

        Astro astro = new Astro();
        astro.setGeneralLevel(generalLevel);
        astro.setLoveLevel(loveLevel);
        astro.setCareerLevel(careerLevel);
        astro.setRichLevel(richLevel);
        astro.setHealthLevel(healthLevel);
        astro.setShortComment(shortComment);
        astro.setGeneralContent(generalContent);
        astro.setLoveContent(loveContent);
        astro.setCareerContent(careerContent);
        astro.setRichContent(richContent);
        astro.setHealthContent(healthContent);
        astro.setRelaxTip(relaxTip);
        astro.setLuckyTip(luckyTip);

        AstroEnum astroEnum = getAstroEnum(astroName);
        if (astroEnum != null) {
            astro.setLabel(astroEnum.getLabel());
            astro.setName(astroEnum.getName());
            astro.setBeginMonth(astroEnum.getBeginMonth());
            astro.setBeginDay(astroEnum.getBeginDay());
            astro.setEndMonth(astroEnum.getEndMonth());
            astro.setEndDay(astroEnum.getEndDay());
        }

        return astro;
    }

    @SneakyThrows
    @Override
    public Astro getAstroYear(String astroName) {
        String url = "https://www.xzw.com/fortune/" + astroName + "/4.html";
        Document document = Jsoup.connect(url).get();
        System.out.println("send request to " + url);
        Elements lis = document.getElementsByClass("c_main").get(0).select("dd ul li");

        Integer generalLevel = getNumber(lis.get(0).select("span em").get(0).attr("style")) / 16;
        Integer loveLevel = getNumber(lis.get(1).select("span em").get(0).attr("style")) / 16;
        Integer careerLevel = getNumber(lis.get(2).select("span em").get(0).attr("style")) / 16;
        Integer richLevel = getNumber(lis.get(3).select("span em").get(0).attr("style")) / 16;
        Integer healthLevel = getNumber(lis.get(4).select("span em").get(0).attr("style")) / 16;
        String shortComment = lis.get(5).ownText();

        Elements contents = document.getElementsByClass("c_cont").get(0).select("p span");

        String generalContent = contents.get(0).ownText();
        String loveContent = contents.get(1).ownText();
        String careerContent = contents.get(2).ownText();
        String richContent = contents.get(3).ownText();
        String healthContent = contents.get(4).ownText();
        String luckyTip = contents.get(5).ownText();

        Astro astro = new Astro();
        astro.setGeneralLevel(generalLevel);
        astro.setLoveLevel(loveLevel);
        astro.setCareerLevel(careerLevel);
        astro.setRichLevel(richLevel);
        astro.setHealthLevel(healthLevel);
        astro.setShortComment(shortComment);
        astro.setGeneralContent(generalContent);
        astro.setLoveContent(loveContent);
        astro.setCareerContent(careerContent);
        astro.setRichContent(richContent);
        astro.setHealthContent(healthContent);
        astro.setLuckyTip(luckyTip);

        AstroEnum astroEnum = getAstroEnum(astroName);
        if (astroEnum != null) {
            astro.setLabel(astroEnum.getLabel());
            astro.setName(astroEnum.getName());
            astro.setBeginMonth(astroEnum.getBeginMonth());
            astro.setBeginDay(astroEnum.getBeginDay());
            astro.setEndMonth(astroEnum.getEndMonth());
            astro.setEndDay(astroEnum.getEndDay());
        }

        return astro;
    }

    @SneakyThrows
    @Override
    public Astro getAstroLove(String astroName) {
        String url = "https://www.xzw.com/fortune/" + astroName + "/5.html";
        Document document = Jsoup.connect(url).get();
        System.out.println("send request to " + url);
        Elements lis = document.getElementsByClass("c_main").get(0).select("dd ul li");

        Integer generalLevel = getNumber(lis.get(0).select("span em").get(0).attr("style")) / 16;
        Integer boyLevel = getNumber(lis.get(1).select("span em").get(0).attr("style")) / 16;
        Integer girlLevel = getNumber(lis.get(2).select("span em").get(0).attr("style")) / 16;
        String shortComment = lis.get(3).ownText();

        Elements contents = document.getElementsByClass("c_cont").get(0).select("p span");

        String generalContent = contents.get(0).ownText();
        String girlContent = contents.get(1).ownText();
        String boyContent = contents.get(2).ownText();

        Astro astro = new Astro();
        astro.setGeneralLevel(generalLevel);
        astro.setBoyLevel(boyLevel);
        astro.setGirlLevel(girlLevel);
        astro.setShortComment(shortComment);
        astro.setGeneralContent(generalContent);
        astro.setBoyContent(boyContent);
        astro.setGirlContent(girlContent);

        AstroEnum astroEnum = getAstroEnum(astroName);
        if (astroEnum != null) {
            astro.setLabel(astroEnum.getLabel());
            astro.setName(astroEnum.getName());
            astro.setBeginMonth(astroEnum.getBeginMonth());
            astro.setBeginDay(astroEnum.getBeginDay());
            astro.setEndMonth(astroEnum.getEndMonth());
            astro.setEndDay(astroEnum.getEndDay());
        }

        return astro;
    }

}
